<%@page import="tu.java.login.User"%>
<%@page import="tu.java.hall.HallSeat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.Vector, tu.java.projection.Projection, tu.java.utils.UIUtils, tu.java.movie.Movie, tu.java.hall.Hall"%>
    
<%@include file="template/header.jspf" %>

<%
if(!Menu.checkUserAcess(request, 7)){
	response.sendRedirect(request.getContextPath());
	return;
}
User user = (User) session.getAttribute("userObj");
int userId = user.getUserId();

if (request.getParameter("mode") == null) {
	response.sendRedirect("halls.jsp?mode=search");
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("save")) {
	Hall h = Hall.searchHalls("", Integer.parseInt(request.getParameter("id"))).get(0);
	h.setHallName(request.getParameter("name"));
	h.setHallDescription(request.getParameter("description"));
	h.editHall(userId);
	response.sendRedirect("halls.jsp?mode=edit&id=" + request.getParameter("id"));
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("addhall")) {
	Hall h = new Hall();
	h.setHallName(request.getParameter("name"));
	h.setHallDescription(request.getParameter("description"));
	h.addHall(userId);
	response.sendRedirect("halls.jsp?mode=search");
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("delete")) {
	Hall h = Hall.searchHalls("", Integer.parseInt(request.getParameter("id"))).get(0);
	h.deleteHall();
	response.sendRedirect("halls.jsp?mode=search&name=" + request.getParameter("name") + "&submit");
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("addseat")) {
	HallSeat.addHallSeat(Integer.parseInt(request.getParameter("seatNum")), Integer.parseInt(request.getParameter("id")));
	response.sendRedirect("halls.jsp?mode=edit&id=" + request.getParameter("id"));
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("delseat")) {
	HallSeat hs = new HallSeat();
	hs.setHallSeatId(Integer.parseInt(request.getParameter("seatId")));
	hs.deleteHallSeat();
	response.sendRedirect("halls.jsp?mode=edit&id=" + request.getParameter("id"));
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("delallseats")) {
	HallSeat.deleteAllHallSeats(Integer.parseInt(request.getParameter("id")));
	response.sendRedirect("halls.jsp?mode=edit&id=" + request.getParameter("id"));
}
%>

<div class="content">
	
	<% 	
		// ### Search ###
		if (request.getParameter("mode")!=null && request.getParameter("mode").equals("search")) { 
			
			String name = request.getParameter("name")!=null ? request.getParameter("name") : "";

			Vector<Hall> halls = null;
				
	%>
	
	<h2 id="title">Зали</h2>
		
		<form action="halls.jsp?mode=search" method="post" id="search" name="search">
			<table class="formtable">
				<tr>
					<td align="right">Име:</td>
					<td align="left">
						<input type="text" id="name" name="name" value="<%=name%>"/>
					</td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" id="submit" name="submit" value="Търси"/>&nbsp;<input type="button" id="add" name="add" value="Добави нова" onclick="window.location = 'halls.jsp?mode=add';"/></td>
				</tr>
			</table>
		</form>
		
		
		<% 
			if (request.getParameter("submit")!=null) {
				
				halls = Hall.searchHalls("%" + name + "%", 0);
			}
		
			if (halls != null) {
		%>
		<table class="resulttable">
			<tr>
				<th width="100">Име</th>
				<th width="300">Описание</th>
				<th width="100">Места</th>
				<th width="100">Действие</th>
			</tr>
			<%for (int i=0; i<halls.size(); i++) { %>
			<tr>
				<td><%=halls.get(i).getHallName()%></td>
				<td><%=halls.get(i).getHallDescription()%></td>
				<td><%=halls.get(i).getHallSeatCount()%></td>
				<td>
					<a href="halls.jsp?mode=edit&id=<%=halls.get(i).getHallId()%>">Редактирай</a>&nbsp;
					<% if (halls.get(i).getHallSeatCount() == 0) {%>
					<a href="#" 
						onclick="confirmMsg('Сигурни ли сте, че искате да изтриете тази зала?','halls.jsp?mode=delete&id=<%=halls.get(i).getHallId()%>&name=<%=name%>');">
					Изтрий</a>
					<%} %>
				</td>
			</tr>
			<%} %>
		</table>
		<%=halls.size()==0 ? "<div class=\"error\">Няма намерени резултати!</div>":""%>
			<%} %>
		
	<%} %>
		
		
	<% 	
		// ### Edit ###
		if (request.getParameter("mode")!=null && request.getParameter("mode").equals("edit")) { 
			
			int id = Integer.parseInt(request.getParameter("id"));
			
			Hall hall = Hall.searchHalls("", id).get(0);

	%>
		
		<h2 id="title">Редактиране на зала</h2>
		
		<form action="halls.jsp?mode=save" method="post" id="edit" name="edit" onsubmit="return validateHallDetails();">
			<input type="hidden" id="id" name="id" value="<%=id%>">
			<table class="formtable">
				<tr>
					<td align="right">Име:</td>
					<td align="left"><input type="text" id="name" name="name" value="<%=hall.getHallName()%>"/></td>
				</tr>
				<tr>
					<td align="right">Описание:</td>
					<td align="left"><textarea cols="20" rows="5" id="description" name="description"><%=hall.getHallDescription()%></textarea></td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" id="submit" name="submit" value="Запази"/>
				</tr>
			</table>
		</form>
		
		<h3 id="title">Места</h3>
		
		<form action="halls.jsp?mode=addseat" method="post" id="edit" name="edit" onsubmit="return validateHallSeatDetails();">
			<input type="hidden" id="id" name="id" value="<%=id%>">
			<table class="formtable">
				<tr>
					<td align="right">Номер:</td>
					<td align="left">
						<input type="text" id="seatNum" name="seatNum"/> &nbsp; <input type="submit" id="submit" name="submit" value="Добави"/>
					</td>
				</tr>
			</table>
		</form>
		
		<form action="halls.jsp?mode=delseat" method="post" id="edit" name="edit" onsubmit="return true;">
			<input type="hidden" id="id" name="id" value="<%=id%>">
			<table class="formtable">
				<tr>
					<td align="right">Изтрий място:</td>
					<td align="left">
						<select id="seatId" name="seatId">
							<%=UIUtils.printOptions("select CH02_SEAT_ID, CH02_SEAT_NUMBER from TUDB.TH02_SEATS where CH01_HALL_ID = " + id, 0)%>
						</select>
						&nbsp; <input type="submit" id="submit" name="submit" value="Изтрий"/>
						&nbsp; <input type="button" id="delAllSeats" name="delAllSeats" value="Изтрий всички" onclick="confirmMsg('Сигурни ли сте, че искате да изтриете всички места?','halls.jsp?mode=delallseats&id=<%=id%>');"/>
					</td>
				</tr>
			</table>
		</form>
		
		<a href="halls.jsp?mode=search">&lt;&lt; Търсене</a>
	
	<%} %>
	
	
	<% 	
		// ### Add ###
		if (request.getParameter("mode")!=null && request.getParameter("mode").equals("add")) { 
			
	%>
		
		<h2 id="title">Добавяне на зала</h2>
		
		<form action="halls.jsp?mode=addhall" method="post" id="edit" name="edit" onsubmit="return validateHallDetails();">
			<table class="formtable">
				<tr>
					<td align="right">Име:</td>
					<td align="left"><input type="text" id="name" name="name"/></td>
				</tr>
				<tr>
					<td align="right">Описание:</td>
					<td align="left"><textarea cols="20" rows="5" id="description" name="description"></textarea></td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" id="submit" name="submit" value="Добави"/>
				</tr>
			</table>
		</form>

		<a href="halls.jsp?mode=search">&lt;&lt; Търсене</a>
	
	<%} %>
		

</div>

<%@include file="template/footer.jspf" %>